//
//  ZMSendTextV.m
//  YYFMMall
//
//  Created by chenzm on 2019/9/16.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "ZMSendTextV.h"

#define sendIphone_W [UIScreen mainScreen].bounds.size.width
#define sendIphone_H (kIsIPhoneX ? ([UIScreen mainScreen].bounds.size.height -13 ) :[UIScreen mainScreen].bounds.size.height)
@interface ZMSendTextV()<UITextViewDelegate>


@end

@implementation ZMSendTextV{
    CGFloat showBgV_h;
}


#pragma mark - 赋值
-(void)setIsShowLeftBtn:(BOOL)isShowLeftBtn{
    _isShowLeftBtn = isShowLeftBtn;
    self.leftBtn.hidden = !isShowLeftBtn;
}

#pragma mark - Public Methods
+(instancetype)registerSendTextV:(UIView *)view{
    ZMSendTextV *sendV = [[ZMSendTextV alloc]initWithFrame:CGRectMake(0, 0, sendIphone_W, sendIphone_H)];
    if (view) {
        [view addSubview:sendV];
    }else{
        [kWindow addSubview:sendV];
    }
    return sendV;
}

- (void)showView {
    [self.textV becomeFirstResponder];
}

- (void)dismiss {
    self.textV.text = @"";
    [self removeFromSuperview];
}

#pragma mark - PrivateMethods
-(void)leftAction{
    !self.leftBtnBlock?:self.leftBtnBlock(self.textV.text);
    [self dismiss];
}

-(void)rightAction{
    !self.rightBtnBlock?:self.rightBtnBlock(self.textV.text);
    [self dismiss];
}

-(void)tapView{
    [self dismiss];
}

#pragma mark - Intial
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder: aDecoder]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

///基本数据配置
-(void)setUpBaseData{
    showBgV_h = 125;
    [self addNotify];
}

///移除监听
- (void)dealloc {
    [self removeNotify];
}

-(void)addNotify{
    //键盘
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

-(void)removeNotify{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    //获取改变尺寸后的键盘的frame
    CGRect endKeyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = endKeyboardRect.size.height;
    self.showBgV.mj_y = sendIphone_H - showBgV_h - height;
}

- (void)keyboardWillHide:(NSNotification *)notification{
    self.showBgV.mj_y = sendIphone_H;
    [self tapView];
}


///控件添加
-(void)setUpUI{
    [self bgView];
    [self showBgV];
    [self textV];
    [self placeHolderLbl];
    [self leftBtn];
    [self rightBtn];
}

#pragma mark - delegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length) {
        self.placeHolderLbl.hidden = YES;
    } else {
        self.placeHolderLbl.hidden = NO;
    }
}
#pragma mark - lazyload
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.7;
        [self addSubview:_bgView];
        _bgView.frame = CGRectMake(0, 0, sendIphone_W, sendIphone_H);
        //点击其他区域时取消显示
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        [_bgView addGestureRecognizer:tapGesture];
    }
    return _bgView;
}

-(UIView *)showBgV{
    if (!_showBgV) {
        _showBgV = [UIView new];
        _showBgV.backgroundColor = [UIColor whiteColor];
        [self addSubview:_showBgV];
        _showBgV.frame = CGRectMake(0, sendIphone_H, sendIphone_W, showBgV_h);
    }
    return _showBgV;
}

-(UITextView *)textV{
    if (!_textV) {
        _textV = [UITextView new];
        _textV.backgroundColor = kHexColor(0xE6E6E6);
        _textV.delegate = self;
        [self.showBgV addSubview:_textV];
        _textV.frame = CGRectMake(12,CGRectGetMinX(self.showBgV.bounds) + 12, CGRectGetWidth(self.showBgV.frame) - 12*2, 65);
        [ZMSpeedy zm_changeControl:_textV cornerRadius:3 borderWidth:0 borderColor:nil mask:YES];
    }
    return _textV;
}

-(UILabel *)placeHolderLbl{
    if (!_placeHolderLbl) {
        _placeHolderLbl = [UILabel new];
        _placeHolderLbl.backgroundColor = [UIColor clearColor];
        _placeHolderLbl.font = [UIFont systemFontOfSize:14];
        _placeHolderLbl.textColor = kHexColor(0x999999);
        _placeHolderLbl.text = @"请输入";
        _placeHolderLbl.hidden = NO;
        [self.showBgV addSubview:_placeHolderLbl];
        _placeHolderLbl.frame = CGRectMake(16,16, CGRectGetWidth(self.showBgV.frame) - 16*2, 20);
    }
    return _placeHolderLbl;
}

-(UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setTitle:@"" forState:UIControlStateNormal];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.backgroundColor = [UIColor clearColor];
        [_leftBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        _leftBtn.hidden = YES;
        [self.showBgV addSubview:_leftBtn];
        _leftBtn.frame = CGRectMake(15, CGRectGetMaxY(self.showBgV.bounds) - 26 - 10, 70, 26);
        [ZMSpeedy zm_changeControl:_leftBtn cornerRadius:_leftBtn.mj_h/2 borderWidth:0 borderColor:nil mask:YES];

    }
    return _leftBtn;
}
-(UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setTitle:@"发送" forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.backgroundColor = kMainColor;
        [self.showBgV addSubview:_rightBtn];
        _rightBtn.frame = CGRectMake(CGRectGetMaxX(self.showBgV.bounds) - 70 - 15, CGRectGetMaxY(self.showBgV.bounds) - 26 - 10, 70, 26);
        _rightBtn.touchAreaInsets = UIEdgeInsetsMake(10, 20, 10, 15);
        [ZMSpeedy zm_changeControl:_rightBtn cornerRadius:_rightBtn.mj_h/2 borderWidth:0 borderColor:nil mask:YES];
    }
    return _rightBtn;
}


@end
